program chapter11_10
    implicit none
    integer i,p1,p2,p3,p4,n,order
    integer a(100)
    do i=1,20
        a(i)=i
    end do !from 1 to 20
    print *, 'input p1,p2,p3,p4 in a rising order'
    read (*,*) p1,p2,p3,p4
    if (p1 .LT. p2 .AND. p2 .LT. p3 .AND. p3 .LT. p4) then
        !order(a,p1,p2,p3,p4)
        do n=1,20
            print*,
        end do
    else
        print*, 'wrong order, input again'
    end if
end program

integer function order(a,p1,p2,p3,p4)

    dimension a(100)
    integer j,k
    if (p4 .eq. 20) then
        do j=p1+1,p2-1
            a(20+j-p1)=a(j)
        end do
        do j=p2,p3-1
            a(40+j+1-p2)=a(j)
        end do
        do j=p3,p4-1
            a(60+j+1-p3)=a(j)
        end do
        do j=p4,20
            a(80+j+1-p4)=a(j)
        end do
    else!p4 <20
        do k=p1+1,p2-1
            a(20+k-p1)=a(k)
        end do
        do k=p2,p3-1
            a(40+k+1-p2)=a(k)
        end do
        do k=p3,p4-1
            a(60+k+1-p3)=a(k)
        end do
    end if

end function
